package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.ActivityMapper;
import com.study.happystudy.mapper.CourseMapper;
import com.study.happystudy.mapper.MessageMapper;
import com.study.happystudy.mapper.UserMapper;
import com.study.happystudy.po.*;
import com.study.happystudy.service.MessageService;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    MessageMapper mp;
    @Resource
    UserMapper up;
    @Resource
    CourseMapper cm;
    @Resource
    ActivityMapper am;

    int checkUser(int sid, int rid) {
        User u1 = up.findUserById(sid), u2 = up.findUserById(rid);
        if (u1 == null && u2 == null) return -1;
        if (u1 == null) return -2;
        if (u2 == null) return -3;
        return 1;
    }

    private Message toMessage(Message message) {

        User sender = up.findUserById(message.getSenderId());
        if (sender == null) return null;

        try {
            switch (message.getMessageType()) {
                case "私信", "社区":
                    message.setSenderAvatar(sender.getUserHeadPortrait());
                    message.setSenderNickname(sender.getUserNickname());
                    return message;
                case "系统":
                    message.setSenderAvatar("https://img1.baidu.com/it/u=3234140817,459742051&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
                    message.setSenderNickname("系统");
                    return message;
                case "课程":
                    Course course = cm.findCourseById(message.getSenderId());
                    message.setSenderAvatar(course.getImgUrl());
                    message.setSenderNickname(course.getCourseName());
                    return message;
                case "活动":
                    Activity activity = am.getActivityById(message.getSenderId());
                    message.setSenderAvatar(activity.getImageUrl());
                    message.setSenderNickname(activity.getTitle());
                    return message;
                default:
                    return null;
            }
        } catch (Exception e) {
            ColorChange.Red("Fuck:" + e.getMessage());
            return null;
        }
    }

    @Override
    public int sendPrivateMessage(int senderId, int receiverId, String messageContent) {
        int res = checkUser(senderId, receiverId);
        if (res != 1) return res;
        mp.sendPrivateMessage(senderId, receiverId, messageContent);
        ColorChange.Blue("发送私信消息成功");
        return 1;
    }

    @Override
    public int revokePrivateMessage(int messageId) {
        Message message = mp.getMessageById(messageId);
        if (message == null) return -1;
        mp.revokePrivateMessage(messageId);
        return 1;
    }

    @Override
    public Message getMessageById(int messageId) {
        return toMessage(mp.getMessageById(messageId));
    }

    @Override
    public int deleteMessageById(int messageId) {
        Message message = mp.getMessageById(messageId);
        if (message == null) return -1;
        mp.deleteMessageById(messageId);
        return 1;
    }

    @Override
    public int sendSystemMessage(int senderId, int receiverId, String messageContent) {
        int res = checkUser(senderId, receiverId);
        if (res != 1) return res;
        mp.sendSystemMessage(senderId, receiverId, messageContent);
        ColorChange.Blue("发送系统消息成功");
        return 1;
    }

    @Override
    public int sendCourseMessage(int senderId, int receiverId, String messageContent) {
        User u = up.findUserById(receiverId);
        if (u == null) return -1;
        mp.sendCourseMessage(senderId, receiverId, messageContent);
        ColorChange.Blue("发送课程消息成功");
        return 1;
    }

    @Override
    public int sendActivityMessage(int senderId, int receiverId, String messageContent) {
        User u = up.findUserById(receiverId);
        if (u == null) return -1;
        mp.sendActivityMessage(senderId, receiverId, messageContent);
        ColorChange.Blue("发送活动消息成功");
        return 1;
    }

    @Override
    public int sendCommunityMessage(int senderId, int receiverId, String messageContent) {
        User u = up.findUserById(receiverId);
        if (u == null) return -1;
        mp.sendCommunityMessage(senderId, receiverId, messageContent);
        ColorChange.Blue("发送社区消息成功");
        return 1;
    }

    @Override
    public List<Message> searchMessagesByKeyword(int userId, String keyword) {
        List<Message> messages = mp.searchMessagesByKeyword(userId, keyword);

        if (messages == null) {
            return null;
        }

        messages.replaceAll(this::toMessage);

        return messages;
    }

    public List<Message> getMessagesByUserId(int userId, String messageType) {

        List<Message> messages = mp.getMessagesByUserId(userId, messageType);

        if (messages == null) {
            return null;
        }

        try {
            messages.replaceAll(this::toMessage);
        } catch (Exception e) {
            ColorChange.Red("Fuck:" + e.getMessage());
            return new ArrayList<>();
        }

        return messages;
    }

    public List<Message> getPrivateMessage(int userId1, int userId2) {
        List<Message> messages = mp.getPrivateMessage(userId1, userId2);
        return messages.stream().map(this::toMessage).collect(Collectors.toList());
    }

   public int getPrivateMessageUnreadCount(@Param("userId1") int userId1, @Param("userId2") int userId2){
        return mp.getPrivateMessageUnreadCount(userId1, userId2);
   }
   public int getSystemMessageUnreadCount(@Param("userId") int userId){
        return mp.getSystemMessageUnreadCount(userId);
   }
   public int getCourseMessageUnreadCount(@Param("userId") int userId){
        return mp.getCourseMessageUnreadCount(userId);
   }
   public int getActivityMessageUnreadCount(@Param("userId") int userId){
        return mp.getActivityMessageUnreadCount(userId);
   }
   public int getCommunityMessageUnreadCount(@Param("userId") int userId){
        return mp.getCommunityMessageUnreadCount(userId);
   }
   public int privateReadAll(@Param("userId1") int userId1, @Param("userId2") int userId2){
        return mp.privateReadAll(userId1, userId2);
   }
   public int systemReadAll(@Param("userId") int userId){
        return mp.systemReadAll(userId);
   }
   public int courseReadAll(@Param("userId") int userId){
        return mp.courseReadAll(userId);
   }
   public int activityReadAll(@Param("userId") int userId){
        return mp.activityReadAll(userId);
   }
   public int communityReadAll(@Param("userId") int userId){
        return mp.communityReadAll(userId);
   }
    public String getLastMessage(@Param("userId1") int userId1, @Param("userId2") int userId2){
        return mp.getLastMessage(userId1, userId2).getMessageContent();
    }
    public PrivateMessageList getPrivateMessageList(int userId1,
                                                    int userId2) {
        User u2 = up.findUserById(userId2);
        String nickname = u2.getUserNickname();
        String avatar = u2.getUserHeadPortrait();
        LastMessage ls = mp.getLastMessage(userId1, userId2);
        if(ls == null){
            return null;
        }
        int unreadCount = mp.getPrivateMessageUnreadCount(userId1, userId2);
        return new PrivateMessageList(userId2, nickname, avatar, ls.getMessageContent(), unreadCount,ls.getSendTime());
    }
    public List<Integer> getFriendsId(int uid){
        return mp.getFriendsId(uid);
    }
}
